package com.zh.api.controller.family;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.domain.UserDO;
import com.zh.common.service.UserService;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.FamilyRelationUserDO;
import com.zh.familyInfo.domain.FamilyUserDTO;
import com.zh.familyInfo.service.FamilyRelationUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zh.familyInfo.domain.FamilyFamilyUserDO;
import com.zh.familyInfo.service.FamilyFamilyUserService;
/**
 * 老人表
 * 
 * @author 马广洋
 * @email 1992lcg@163.com
 * @date 2019-06-12 21:56:34
 */
@Controller
@Api("家庭人员管理")
public class FamilyFamilyUserController extends BaseController{
	@Autowired
	private FamilyFamilyUserService familyFamilyUserService;

	@Autowired
	private FamilyRelationUserService familyRelationUserService;

	@Autowired
	private UserService userService;

	@Log("获取当前登录家庭信息")
	@ResponseBody
	@RequestMapping(value = "listFamilyUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listFamilyUser(@RequestBody Map<String, Object> params){
		if (CheckEmptyUtil.isEmptys(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据--条件当前用户家庭号
			params.put("number", getFamilyUser().getNumber());
			//条件当前用户登录的家庭户号
			Query query = new Query(params);
			List<FamilyFamilyUserDO> familyFamilyUserList = familyFamilyUserService.list(query);
			int total = familyFamilyUserService.count(query);
			PageUtils pageUtils = new PageUtils(familyFamilyUserList, total);
			return ResultVO.success(pageUtils);
	}

	@Log("根据id编辑家庭个人信息【不包含登录信息】")
	@ResponseBody
	@RequestMapping(value = "editFamilyUserById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editFamilyUserById(@RequestBody FamilyFamilyUserDO familyFamilyUserDO){
		if(familyFamilyUserDO==null || CheckEmptyUtil.isEmptys(familyFamilyUserDO.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		FamilyFamilyUserDO familyFamilyUser = familyFamilyUserService.get(familyFamilyUserDO.getId());
	    return ResultVO.success(familyFamilyUser);
	}
	/**
	 * 保存
	 */
	@Log("根据扩展类FamilyUserDTO保存家庭个人信息【包含登录信息】")
	@ResponseBody
	@RequestMapping(value = "saveFamilyUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO save(@RequestBody FamilyUserDTO familyUserDTO){
		if(familyUserDTO==null||CheckEmptyUtil.isEmptys(familyUserDTO.getUsername(),familyUserDTO.getPassword(),familyUserDTO.getName())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		FamilyFamilyUserDO  familyUserDO=new FamilyFamilyUserDO();
		BeanUtils.copyProperties(familyUserDTO, familyUserDO);
//		保存家庭用户
		if(familyUserDO!=null && familyFamilyUserService.save(familyUserDO)>0){
			UserDO userDO=new UserDO();
			userDO.setUsername(familyUserDTO.getUsername());
			userDO.setPassword(familyUserDTO.getPassword());
			userDO.setName(familyUserDTO.getName());
//			创建用户为登录id
//			userDO.setUserIdCreate(getUserId());
//				保存登录用户
			if(userService.save(userDO)>0){
				FamilyRelationUserDO familyRelationUserDO=new FamilyRelationUserDO();
				familyRelationUserDO.setUserId(userDO.getUserId());
				familyRelationUserDO.setFamilyId(familyUserDO.getId());
//					保存中间表关系
				if(familyRelationUserService.save(familyRelationUserDO)>0){
					return ResultVO.success();
				}
			}
		}
		return ResultVO.error();
	}
	@Log("更新家庭老人信息")
	@RequestMapping(value = "updateFamilyFamilyUserDO", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultVO updateFamilyFamilyUserDO(@RequestBody  FamilyFamilyUserDO familyFamilyUser){
		if(familyFamilyUser==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		familyFamilyUserService.update(familyFamilyUser);
		return ResultVO.success();
	}
	@Log("删除家庭老人信息")
	@RequestMapping(value = "removeFamilyUserById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultVO remove(@RequestBody  FamilyFamilyUserDO familyFamilyUser){
		if(familyFamilyUser==null || CheckEmptyUtil.isEmptys(familyFamilyUser.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(familyFamilyUserService.remove(familyFamilyUser.getId())>0){
			FamilyRelationUserDO familyRelationUserDO = familyRelationUserService.getFamilyRelationByFamilyId(familyFamilyUser.getId());
		    if(userService.remove(familyRelationUserDO.getUserId())>0){
				if(familyRelationUserService.remove(familyRelationUserDO.getId())>0){
					return ResultVO.success();
				}
			}
		}
		return ResultVO.error();
	}

	@RequestMapping(value = "batchRemoveFamilyUserByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除家庭老人信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
//		批量删除
		String[] strs1=ids.toArray(new String[ids.size()]);
		for(String id:strs1){
			if(familyFamilyUserService.remove(id)>0){
				FamilyRelationUserDO familyRelationUserDO = familyRelationUserService.getFamilyRelationByFamilyId(id);
				if(userService.remove(familyRelationUserDO.getUserId())>0){
					if(familyRelationUserService.remove(familyRelationUserDO.getId())>0){
					}else{
						return ResultVO.error(1,"删除关联表失败！");
					}
				}else{
					return ResultVO.error(1,"删除用户信息失败！");
				}
			}else {
				return ResultVO.error(1,"删除老人信息失败！");
			}
		}
		return ResultVO.success();
	}
}
